package com.cupk.common.base.utils;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author tjy
 */
public class DateFormatUtils {
    private static final Logger log = LoggerFactory.getLogger(DateFormatUtils.class);
    private static final String sdf1Str = "yyyy-MM-dd";
    private static final String sdf2Str = "yyyy-MM-dd HH:mm:ss";
    private static final String sdf3Str = "yyyy";
    private static final String sdf4Str = "MM";
    private static final String sdf5Str = "dd";
    private static final ThreadLocal<SimpleDateFormat> threadLocal = new ThreadLocal();

    public DateFormatUtils() {
    }

    public static void main(String[] args) throws ParseException {
        String s = formatTime(new Date());
        System.out.println(s);
    }

    private static SimpleDateFormat getSimpleDateFormat(String formatStr) {
        SimpleDateFormat sdf = null;
        sdf = (SimpleDateFormat)threadLocal.get();
        if (sdf == null) {
            sdf = new SimpleDateFormat(formatStr);
        }

        return sdf;
    }

    public static String formatTime(Date date) {
        SimpleDateFormat sdf = getSimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String strTime = sdf.format(date);
        return strTime;
    }

    public static String formatTime2(Date date) {
        SimpleDateFormat sdf = getSimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date).replaceAll(" ", "").replaceAll("-", "").replaceAll(":", "");
    }

    public static Date parseTime(String time) throws ParseException {
        SimpleDateFormat sdf = getSimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.parse(time);
    }

    public static Date formatDateTime(Date date) throws ParseException {
        SimpleDateFormat sdf = getSimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.parse(sdf.format(date));
    }

    public static String formatDate(Date date) {
        SimpleDateFormat sdf = getSimpleDateFormat("yyyy-MM-dd");
        String strDate = sdf.format(date);
        return strDate;
    }

    public static String formatDate2(Date date) {
        SimpleDateFormat sdf = getSimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date).replaceAll("-", "");
    }

    public static Date formatDate(String strDate) {
        SimpleDateFormat sdf = getSimpleDateFormat("yyyy-MM-dd");
        Date date = null;

        try {
            date = sdf.parse(strDate);
        } catch (Exception var4) {
            log.info("错误异常信息：", var4);
        }

        return date;
    }

    public static Date getCurrentDate() {
        SimpleDateFormat sdf = getSimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String sysDate = sdf.format(date);
        return formatDate(sysDate);
    }

    public static String getStrCurrentDate() {
        Date date = new Date();
        return formatDate(date);
    }

    public static int checkTime(Date date1, Date date2) {
        if (date1.getTime() > date2.getTime()) {
            return -1;
        } else {
            return date1.getTime() < date2.getTime() ? 1 : 0;
        }
    }

    public static int checkDate(Date date1, Date date2) {
        SimpleDateFormat sdf = getSimpleDateFormat("yyyy-MM-dd");
        Date d2 = null;
        Date d1 = null;

        try {
            d1 = sdf.parse(sdf.format(date1));
            d2 = sdf.parse(sdf.format(date2));
        } catch (ParseException var6) {
            log.info("日期比较过程中出现日期格式化异常！");
            log.info("错误异常信息：", var6);
        }

        if (d1.getTime() > d2.getTime()) {
            return -1;
        } else {
            return d1.getTime() < d2.getTime() ? 1 : 0;
        }
    }

    public static String currentDate() {
        SimpleDateFormat sdf = getSimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String sysDate = sdf.format(date);
        return sysDate;
    }

    public static Date afterThirdMonth() {
        SimpleDateFormat sdf = getSimpleDateFormat("yyyy-MM-dd");
        Date date = getCurrentDate();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(5, 90);
        date = cal.getTime();
        String stringDate = sdf.format(date);
        return formatDate(stringDate);
    }

    public static String getStrNextDate(Date date) {
        SimpleDateFormat sdf = getSimpleDateFormat("yyyy-MM-dd");
        String str = "";
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(5, 1);
        date = cal.getTime();
        str = sdf.format(date);
        return str;
    }

    public static Date getNextDate(String strDate) {
        Date date = null;
        Calendar cal = Calendar.getInstance();
        cal.setTime(formatDate(strDate));
        cal.add(5, 1);
        date = cal.getTime();
        return date;
    }

    public static Date getBeforeDate(Date date, int day) {
        Date result = null;
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(5, day);
        result = cal.getTime();
        return result;
    }

    public static String getLastDayOfMonth() {
        SimpleDateFormat sdf = getSimpleDateFormat("yyyy-MM-dd");
        String str = "";
        Calendar lastDate = Calendar.getInstance();
        lastDate.set(5, 1);
        lastDate.add(2, 1);
        lastDate.add(5, -1);
        str = sdf.format(lastDate.getTime());
        return str;
    }

    public static String getFirstDayOfMonth() {
        SimpleDateFormat sdf = getSimpleDateFormat("yyyy-MM-dd");
        String str = "";
        Calendar lastDate = Calendar.getInstance();
        lastDate.set(5, 1);
        str = sdf.format(lastDate.getTime());
        return str;
    }

    public static String getNowTimeString() {
        SimpleDateFormat sdf = getSimpleDateFormat("yyyy-MM-dd");
        return sdf.format(new Date());
    }

    public static String getFormatDate(String dateStr) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String date = format.format(sdf.parse(dateStr));
            return date;
        } catch (Exception var4) {
            return null;
        }
    }

    public static String getDate(Date date, String formatStr) {
        return getFormatDate(date, formatStr);
    }

    public static String getFormatDate(Date date, String pattern) {
        if (pattern == null || pattern.length() < 1) {
            pattern = "yyyy-MM-dd";
        }

        try {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            String strDate = sdf.format(date);
            return strDate;
        } catch (Exception var4) {
            log.info("日期格转换失败！");
            return null;
        }
    }

    public static Date formatDate(Date date, String pattern) {
        SimpleDateFormat sdf = getSimpleDateFormat("yyyy-MM-dd");

        try {
            return sdf.parse(sdf.format(date));
        } catch (ParseException var4) {
            log.info("错误异常信息：", var4);
            return date;
        }
    }

    public static Date getNextDate(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(5, 1);
        date = cal.getTime();
        return formatDate(date, "yyyy-MM-dd");
    }

    public static Date getNextDateTime(Date date) throws ParseException {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(5, 1);
        date = cal.getTime();
        return formatDateTime(date);
    }

    public static String currentYear() {
        SimpleDateFormat sdf = getSimpleDateFormat("yyyy");
        Date date = new Date();
        String sysDate = sdf.format(date);
        return sysDate;
    }

    public static String currentMonth() {
        SimpleDateFormat sdf = getSimpleDateFormat("MM");
        Date date = new Date();
        String sysDate = sdf.format(date);
        return sysDate;
    }

    public static String currentDay() {
        SimpleDateFormat sdf = getSimpleDateFormat("dd");
        Date date = new Date();
        String sysDate = sdf.format(date);
        return sysDate;
    }

    public static int getCurrentMonthDay() {
        Calendar a = Calendar.getInstance();
        a.set(5, 1);
        a.roll(5, -1);
        int maxDate = a.get(5);
        return maxDate;
    }

    public static int getSumDay(String date) {
        int year = Integer.parseInt(date.substring(0, 4));
        int month = Integer.parseInt(date.substring(5, date.length()));
        int day = 0;
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                day = 31;
                break;
            case 2:
                day = year % 4 == 0 ? 29 : 28;
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                day = 30;
        }

        return day;
    }

    public static long getLongTime(String date1, String date2) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Long time = null;

        try {
            time = dateFormat.parse(date1).getTime() - dateFormat.parse(date2).getTime();
        } catch (ParseException var5) {
            log.info("错误异常信息：", var5);
        }

        return time;
    }

    public static String formatTime(long ms) {
        int ss = 1000;
        int mi = ss * 60;
        int hh = mi * 60;
        int dd = hh * 24;
        long day = ms / (long)dd;
        long hour = (ms - day * (long)dd) / (long)hh;
        long minute = (ms - day * (long)dd - hour * (long)hh) / (long)mi;
        long second = (ms - day * (long)dd - hour * (long)hh - minute * (long)mi) / (long)ss;
        long milliSecond = ms - day * (long)dd - hour * (long)hh - minute * (long)mi - second * (long)ss;
        String strDay = day < 10L ? "0" + day : "" + day;
        String strHour = hour < 10L ? "0" + hour : "" + hour;
        String strMinute = minute < 10L ? "0" + minute : "" + minute;
        String strSecond = second < 10L ? "0" + second : "" + second;
        String strMilliSecond = milliSecond < 10L ? "0" + milliSecond : "" + milliSecond;
        if (milliSecond < 100L) {
            (new StringBuilder()).append("0").append(strMilliSecond).toString();
        } else {
            (new StringBuilder()).append("").append(strMilliSecond).toString();
        }

        if (!"00".equals(strDay)) {
            return strDay + "天" + strHour + "小时" + strMinute + " 分钟 " + strSecond + " 秒";
        } else {
            return !"00".equals(strHour) ? strHour + "小时" + strMinute + " 分钟 " + strSecond + " 秒" : strMinute + " 分钟 " + strSecond + " 秒";
        }
    }

    public static List<Date> getDatesBetweenTwoDate(Date beginDate, Date endDate) {
        List<Date> dateList = new ArrayList();
        dateList.add(beginDate);
        Calendar cal = Calendar.getInstance();
        cal.setTime(beginDate);

        while(true) {
            cal.add(5, 1);
            if (!endDate.after(cal.getTime())) {
                dateList.add(endDate);
                return dateList;
            }

            dateList.add(cal.getTime());
        }
    }

    public static List<Date> getDatesByBeginAndLength(Date beginDate, int length) {
        List<Date> dateList = new ArrayList();
        dateList.add(beginDate);
        Calendar cal = Calendar.getInstance();
        cal.setTime(beginDate);
        Calendar cal2 = Calendar.getInstance();
        cal.setTime(beginDate);
        cal2.add(5, length);
        Date endDate = cal2.getTime();

        while(true) {
            cal.add(5, 1);
            if (!endDate.after(cal.getTime())) {
                return dateList;
            }

            dateList.add(cal.getTime());
        }
    }

    public static List<String> getDateStrsByBeginAndLength(Date beginDate, int length) {
        SimpleDateFormat sdf = getSimpleDateFormat("yyyy-MM-dd");
        List<String> result = new ArrayList();
        List<Date> dates = getDatesByBeginAndLength(beginDate, length);
        Iterator var5 = dates.iterator();

        while(var5.hasNext()) {
            Date date = (Date)var5.next();
            result.add(sdf.format(date));
        }

        return result;
    }

    public static List<Date> getDatesByEndAndLength(Date endDate, int length) {
        List<Date> dateList = new ArrayList();
        Calendar cal = Calendar.getInstance();
        cal.setTime(endDate);
        Calendar cal2 = Calendar.getInstance();
        cal.setTime(endDate);
        cal2.add(5, length);
        Date beginDate = cal2.getTime();
        dateList.add(beginDate);

        while(true) {
            cal2.add(5, 1);
            if (!endDate.after(cal2.getTime()) || endDate.equals(cal2.getTime())) {
                return dateList;
            }

            dateList.add(cal2.getTime());
        }
    }

    public static List<String> getDateStrsByEndAndLength(Date endDate, int length) {
        SimpleDateFormat sdf = getSimpleDateFormat("yyyy-MM-dd");
        List<String> result = new ArrayList();
        List<Date> dates = getDatesByEndAndLength(endDate, length);
        Iterator var5 = dates.iterator();

        while(var5.hasNext()) {
            Date date = (Date)var5.next();
            result.add(sdf.format(date));
        }

        return result;
    }

    public static List<String> getDayOfMonth(List<Date> list) {
        List<String> dayList = new ArrayList();
        Calendar ca = Calendar.getInstance();
        Iterator var4 = list.iterator();

        while(var4.hasNext()) {
            Date date = (Date)var4.next();
            ca.setTime(date);
            String day = String.valueOf(ca.get(5));
            dayList.add(day);
        }

        return dayList;
    }

    public static List<String> getDayOfWeek(List<Date> list) {
        List<String> dayList = new ArrayList();
        Calendar ca = Calendar.getInstance();
        Iterator var4 = list.iterator();

        while(var4.hasNext()) {
            Date date = (Date)var4.next();
            ca.setTime(date);
            String day = String.valueOf(ca.get(7));
            dayList.add(day);
        }

        return dayList;
    }

    public static Date getDayOfDifference(Date date, int difference) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(6, difference);
        return calendar.getTime();
    }

    public static String getDayByMonth(String datetime) {
        StringBuffer buffer = new StringBuffer();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Calendar calendar = new GregorianCalendar();

        try {
            Date date = sdf.parse(datetime);
            calendar.setTime(date);
            int day = calendar.getActualMaximum(5);
            if (day < 10) {
                buffer = buffer.append("0").append(String.valueOf(day));
            } else {
                buffer = buffer.append(String.valueOf(day));
            }
        } catch (ParseException var6) {
            log.info("错误异常信息：", var6);
        }

        return buffer.toString();
    }

    public static boolean regexTest(String dateStr, String formatStr) throws Exception {
        String regex = "(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29) ";
        String regex2 = "(((0[1-9]|[12][0-9]|3[01])/((0[13578]|1[02]))|((0[1-9]|[12][0-9]|30)/(0[469]|11))|(0[1-9]|[1][0-9]|2[0-8])/(02))/([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3}))|(29/02/(([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00)))";
        if ("yyyy-MM-dd".equals(formatStr) && StringUtils.isNotBlank(dateStr)) {
            return dateStr.matches(regex);
        } else if ("dd/MM/yyyy".equals(formatStr)) {
            return dateStr.matches(regex2);
        } else {
            throw new Exception("不支持的格式类型！(日期格式类型为“yyyy-MM-dd”或“dd/MM/yyyy”)");
        }
    }

    public static String getString(long milliseconds) {
        StringBuffer rtnStr = new StringBuffer();
        long seconds = milliseconds / 1000L;
        long min = seconds / 60L;
        long ho = min / 60L;
        if (ho > 0L) {
            rtnStr.append(ho + "小时");
        }

        if (min > 0L) {
            rtnStr.append(min % 60L + "分");
        }

        rtnStr.append(seconds % 60L + "秒");
        return rtnStr.toString();
    }

    public static long getTimeDifference(String strTime1, String strTime2, int type) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long result = 0L;

        try {
            Date nowDate = df.parse(strTime1);
            Date compareDate = df.parse(strTime2);
            long diff = nowDate.getTime() - compareDate.getTime();
            long day = diff / 86400000L;
            long hour = diff / 3600000L - day * 24L;
            long min = diff / 60000L - day * 24L * 60L - hour * 60L;
            if (type == 1) {
                result = day;
            }

            if (type == 2) {
                result = hour;
            }

            if (type == 3) {
                result = min;
            }

            log.info("" + day + "天" + hour + "小时" + min + "分");
        } catch (Exception var16) {
            log.info("错误异常信息：", var16);
        }

        return result;
    }
}